package teamproject.graphEditor;

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import edu.uci.ics.jung.algorithms.layout.Layout;

public class ScreenShots<E, V> {
	public void takeJpg(Layout<V, E> layout, File file, int width, int height,
			int xPos, int yPos) {
		try {
			Robot robot = new Robot();
			Rectangle captureSize = new Rectangle(xPos, yPos, width, height);
			BufferedImage bufferedImage = robot
					.createScreenCapture(captureSize);

			String path = file.getPath();
			String path2[] = path.split("\\.");
			path = path2[0];
			String memory = path2[0] + "_";
			file = new File(path + ".jpg");

			int errorNumber = -1;
			while (file.exists()) {
				++errorNumber;
				path = memory;
				path = path + errorNumber;
				file = new File(path + ".jpg");
			}

			file = new File(path + ".jpg");
			ImageIO.write(bufferedImage, "jpg", file);

			// System.exit(0);
		} catch (HeadlessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}